<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class Admin extends Base
{
    private $mod;
    protected $beforeActionList = [
        'beforeIndex'=>['only'=>'index'],
        'beforeEdit' =>['only'=>'add,edit']
    ];
    public function _initialize() {
        parent::_initialize();
        $this->mod = model('admin');
    }
    public function beforeIndex()
    {
        $big_menu = array(
            'title' => '添加管理员',
            'iframe' => url('Admin/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '210'
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('roleid',session('adminInfo.role'));
        $this->assign('superroleid',config('SUPERADMIN_ROLEID'));
        //return $this->fetch();
    }
    public function beforeEdit(){
        $lists = model('role')->field('id,title')->where(['status'=>1])->select();
        if(session('roleid')!=config('SUPERADMIN_ROLEID')){
            foreach($lists as $k=>$v){
                if($v['id'] < session('roleid')){
                    unset($lists[$k]);
                }
            }
        }
        $this->assign('role',$lists);
    }
    public function delete(){
        \app\manage\model\Admin::event('before_delete',function($obj){
            //不能删除自己的账号
            $id = $obj->id;
            if($id == input('session.adminInfo.id')){
                return false;
            }
            return true;
        });
        parent::delete();
    }
    public function ajaxCheckName() {
        $name = input('param.J_username');
        $id = input('param.id/d');
        if ($this->mod->name_exists($name, $id)) {
            echo 0;
        } else {
            echo 1;
        }
    }
    public function publicSelectUser(){

        $type  = input('param.type');//选择框类型radio checkbox
        $pagesize = 60;
        $total = $this->mod->where('status',1)->count();
        $totalPage = ceil($total/$pagesize);

        $this->assign('type',$type);
        $this->assign('totalPage',$totalPage);
        $this->ajaxReturn(1,'',$this->fetch());
    }
    public function ajaxGetList(){
        $pagesize = 60;
        $page = input('param.page/d',1);
        $keyword = input('param.keyword');
        $map['status'] = 1;
        if($keyword){
            $map['username'] = ['like','%'.$keyword.'%'];
        }
        $lists =  $this->mod->field('id,username')->where($map)->page($page,$pagesize)->select();
        if($lists){
            $this->ajaxReturn(1,'',$lists);
        }else{
            $this->ajaxReturn(0,'无相关数据');
        }
    }
}